﻿
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ET.Monitor.DAL
{
    public class CliqueDAL
    {
        string strSql = "", strErrMsg = "";
        object obj = null;
        DataTable dt = new DataTable();
        bool judge = false;

        InvokeWebService iw = new InvokeWebService();
        string rtdbRelationAddress = "";
        string rtdbRealTimeAddress = "";
        string rtdbNameSpace = "";
        string rtdbJudge = "";

        public CliqueDAL()
        {
            rtdbRelationAddress = ConfigurationManager.ConnectionStrings["RelationURL"].ConnectionString;
            rtdbRealTimeAddress = ConfigurationManager.ConnectionStrings["RealTimeURL"].ConnectionString;
            rtdbNameSpace = ConfigurationManager.ConnectionStrings["NameSpace"].ConnectionString;
            rtdbJudge = ConfigurationManager.ConnectionStrings["Judge"].ConnectionString;
        }

        #region 总容量
        public double GetSumRL()
        {
            double dlVal = 0;
            strSql = "select D_RL,T_TIME,T_ORGID from T_INFO_RL";
            dt = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });

            DataTable dtDistrict = (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { "select T_DISTRICTID from T_BASE_DISTRICT where T_ORGID in(select T_ORGID from T_INFO_PLANT)", strErrMsg });

            if (null != dtDistrict)
            {
                for (int i = 0; i < dtDistrict.Rows.Count; i++)
                {
                    DataRow[] drs = dt.Select("T_ORGID ='" + dtDistrict.Rows[i][0] + "'", "T_TIME desc");
                    if (drs != null)
                        foreach (DataRow dr in drs)
                        {
                            dlVal += Convert.ToDouble(dr["D_RL"].ToString());
                            break;
                        }
                }
            }
            return dlVal;
        }
        #endregion

        #region 实时负荷
        public object GetTotalPower(string id)
        {
            strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    obj = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    obj = GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);
            return obj;
        }
        #endregion

        #region 一段时间电量
        public double GetDL(DateTime st, DateTime et)
        {
            strSql = "select isnull(sum(D_VALUE),0) val from T_INFO_STATISTICS where T_TJID='DL' and T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT where T_ORGID in(select T_ORGID from T_INFO_PLANT)) and T_TIME between '" + st + "' and '" + et + "'";
            return double.Parse(iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg }).ToString());
        }

        public DataTable GetStationMonthTotalDLByTime(DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,sum(D_VALUE) as 合计 from T_INFO_STATISTICS where T_ORGID in(select T_DISTRICTID From T_BASE_DISTRICT) and T_TJID='DL' and T_TIME between '{0}' and '{1}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME) order by 年,月 asc", st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        #endregion

        #region 累计总发电量
        public double GetSumDL(string id)
        {
            strSql = "select T_TOTALELECTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });
            if (obj != null && obj != DBNull.Value)
                if (rtdbJudge == "1")
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValue", new object[] { obj.ToString() })), 2);
                else
                    return GetDouble(Convert.ToDouble(iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetRealValueNoJudge", new object[] { obj.ToString() })), 2);
            else
                return Double.NaN;
        }
        #endregion

        #region 节省标煤量
        public double GetSumBML(string id)
        {
            return GetDouble(GetSumDL(id) / 1000 * 0.36, 2);
        }
        #endregion

        #region 四舍五入
        /// <summary>
        /// 四舍五入
        /// </summary>
        /// <param name="result">要转换的数值</param>
        /// <param name="num">保留位数</param>
        /// <returns></returns>
        public double GetDouble(double result, int num)
        {
            if (!double.IsNaN(result))
            {
                string res = result.ToString();
                string results = "";
                int index = res.IndexOf('.');

                if (res.Length - index == num + 1)
                    return Convert.ToDouble(res);
                else
                {
                    if (index > 0)
                    {
                        index += num;
                        res = res + "000000000000000000";
                        res = res.Remove(0, index + 1);
                        results = result + "000000000000000000";
                        results = results.ToString().Substring(0, index + 1);
                        res = res.Substring(0, 1);

                        string point = "0.";

                        for (int count = 0; count < num - 1; count++)
                        {
                            point += "0";
                        }
                        point += "1";


                        if (Convert.ToInt32(res) > 4)
                        {
                            results = (Convert.ToDouble(results) + Convert.ToDouble(point)).ToString();
                            res = results;
                        }
                        else
                        {
                            res = results;
                        }
                    }
                    else
                    {
                        res += ".";
                        for (int i = 0; i < num; i++)
                        {
                            res += "0";
                        }
                    }
                    return Convert.ToDouble(res);
                }
            }
            else
                return result;
        }
        #endregion

        #region
        public double[] GetPowerLine(string id, DateTime st, DateTime et, int second)
        {
            double[] val = null;
            strSql = "select T_POWERTAG from T_BASE_POINTS_ORG where T_ORGID='" + id + "'";
            obj = iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunSingle", new object[] { strSql, strErrMsg });

            if (obj != null && obj != DBNull.Value)
                //val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStartTimeAndEndTime", new object[] { obj.ToString(), st, et });
                val = (double[])iw.RunWebService(rtdbRealTimeAddress, rtdbNameSpace, "GetHistValueByStep", new object[] { obj.ToString(), st, et, second });
            return val;
        }
        #endregion


        #region 电站电量统计
        public DataTable GetYearTotalDLByTime(DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,sum(D_VALUE) as 合计,T_ORGID from (select s.T_TIME, s.D_VALUE, s.T_ORGID AreaID, d.T_ORGID, s.T_TJID from T_INFO_STATISTICS s inner join T_BASE_DISTRICT d on s.T_ORGID = d.T_DISTRICTID)  t where t.T_TJID='DL' and T_TIME between '{0}' and '{1}' group by datepart(YEAR, T_TIME),T_ORGID order by T_ORGID,年 asc", st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetMonthTotalDLByTime(DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,sum(D_VALUE) as 合计,T_ORGID from (select s.T_TIME, s.D_VALUE, s.T_ORGID AreaID, d.T_ORGID, s.T_TJID from T_INFO_STATISTICS s inner join T_BASE_DISTRICT d on s.T_ORGID = d.T_DISTRICTID)  t where t.T_TJID='DL' and T_TIME between '{0}' and '{1}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME),T_ORGID order by T_ORGID,年,月 asc", st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetDayTotalDLByTime(DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年,datepart(month, T_TIME) as 月,datepart(day, T_TIME) as 日,sum(D_VALUE) as 合计,T_ORGID from (select s.T_TIME, s.D_VALUE, s.T_ORGID AreaID, d.T_ORGID, s.T_TJID from T_INFO_STATISTICS s inner join T_BASE_DISTRICT d on s.T_ORGID = d.T_DISTRICTID)  t where t.T_TJID='DL' and T_TIME between '{0}' and '{1}' group by datepart(YEAR, T_TIME),datepart(month, T_TIME),datepart(day, T_TIME),T_ORGID order by T_ORGID,年,月,日 asc", st, et);
            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }

        public DataTable GetHourTotalDLByTime(DateTime st, DateTime et)
        {
            strSql = string.Format(@"select datepart(YEAR, T_TIME) as 年, datepart(month, T_TIME) as 月, datepart(day, T_TIME) as 日, datepart(hour, T_TIME) as 小时, sum(D_VALUE) as 合计, T_ORGID from (select s.T_TIME, s.D_VALUE, s.T_ORGID AreaID, d.T_ORGID, s.T_TJID from T_INFO_STATISTICS s inner join T_BASE_DISTRICT d on s.T_ORGID = d.T_DISTRICTID)  t where t.T_TJID = 'DL' and T_TIME between '{0}' and '{1}' group by datepart(YEAR, T_TIME), datepart(month, T_TIME), datepart(day, T_TIME), datepart(hour, T_TIME), T_ORGID order by T_ORGID,年, 月, 日, 小时 asc", st, et);

            return (DataTable)iw.RunWebService(rtdbRelationAddress, rtdbNameSpace, "RunDataTable", new object[] { strSql, strErrMsg });
        }
        #endregion
    }
}
